<template>
  <div class="search">
    <div class="search-box-wrapper">
      <search-box ref="searchBox" @query="getQueryChange"></search-box>
    </div>
      <div class="shortcut-wrapper" ref="shortCutWrapper">
        <scroll class="shortcut"  v-show="!query" :data="shortCut" ref="shortCut">
          <div>
            <div class="hotkey">
              <h1 class="title">热门搜索</h1>
              <ul>
                <li class="item" v-for="(item, index) of hotKey" :key="index"
                @click="addQuery(item.k)">{{item.k}}</li>
              </ul>
            </div>
            <div class="search-history" v-show="searchHistory.length">
              <h1 class="title">
                <span class="text">搜索历史</span>
                <span class="clear" @click="handleConfirm"><i class="icon-clear"></i></span>
              </h1>
              <search-list :searches="searchHistory" @deleteOne='deleteSearchHistory' @searchItem="addQuery"></search-list>
            </div>
          </div>
        </scroll>
          <suggest :query="query"
          v-show="query"
          @listScroll="blurInput"
          @select="saveHistory"
          ref="suggest"
          ></suggest>
      </div>
      <confirm text="是否清空所有搜索历史"
      @cancle="handleCancle"
      @confirm="clearSearchHistory"
      ref="confirm"
      ></confirm>
      <transition name="slide">
        <router-view></router-view>
      </transition>
  </div>
</template>
<script>
import SearchBox from 'base/search-box/search-box'
import Suggest from 'components/suggest/suggest'
import {getHotKey} from 'api/search'
import {ERR_OK} from 'api/config'
import {mapActions} from 'vuex'
import SearchList from 'base/search-list/search-list'
import Confirm from 'base/confirm/confirm'
import Scroll from 'base/scroll/scroll'
import {playlistMixin, searchMixin} from 'common/js/mixin'
export default {
  name: 'Search',
  mixins: [playlistMixin, searchMixin],
  components: {
    SearchBox,
    Suggest,
    SearchList,
    Confirm,
    Scroll
  },
  data () {
    return {
      hotKey: []
      // query: ''
    }
  },
  computed: {
    // ...mapGetters(['searchHistory']),
    shortCut () {
      return this.hotKey.concat(this.searchHistory)
    }
  },
  created () {
    this._getHotKey()
  },
  methods: {
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '120px' : ''
      this.$refs.shortCutWrapper.style.bottom = bottom
      this.$refs.suggest.$el.style.bottom = bottom
      this.$refs.suggest.refresh()
      this.$refs.shortCut.refresh()
    },
    handleCancle () {
      this.$refs.confirm.hide()
    },
    handleConfirm () {
      this.$refs.confirm.show()
    },
    // saveHistory () {
    //   this.saveSearchHistory(this.query)
    // },
    // blurInput () {
    //   this.$refs.searchBox.blur()
    // },
    // addQuery (query) {
    //   this.$refs.searchBox.SetQuery(query)
    // },
    // getQueryChange (query) {
    //   this.query = query
    // },
    _getHotKey () {
      getHotKey()
        .then((res) => {
          if (res.code === ERR_OK) {
            this.hotKey = res.data.hotkey.slice(0, 10)
            // console.log(this.hotKey)
          }
        })
    },
    ...mapActions(['clearSearchHistory'])
  },
  watch: {
    query () {
      setTimeout(() => {
        this.$refs.shortCut.refresh()
      }, 20)
    }
  }
}
</script>
<style lang="scss" scoped>
@import 'common/scss/variable.scss';
.search{
  .search-box-wrapper{
    margin: 0.4rem;
  }
  .shortcut-wrapper{
    width: 100%;
    position: fixed;
    top: 3.56rem;
    bottom: 0;
    overflow: hidden;
    .shortcut{
      height: 100%;
      .hotkey{
        margin: 0 0.4rem 0.4rem 0.4rem;
        .title{
          margin-bottom: 0.4rem;
          font-size: $font-size-medium;
          color: $color-text-l;
        }
        ul{
          display: flex;
          justify-content: flex-start;
          flex-wrap: wrap;
          .item{
            padding: 0.1rem 0.2rem;
            margin: 0 0.4rem 0.2rem 0;
            border-radius: 0.12rem;
            background: $color-highlight-background;
            font-size: $font-size-medium-x;
            color: $color-text-d;
          }
        }
      }
    }
    .search-history{
      margin: 0 0.4rem;
      .title{
        display: flex;
        align-items: center;
        height: 0.8rem;
        font-size: $font-size-medium;
        color: $color-text-l;
        .text{
          flex: 1;
        }
        .clear{
          .icon-clear{
            font-size: $font-size-medium;
            color: $color-text-d;
          }
        }
      }
    }
  }
  .slide-enter-active, .slide-leave-active{
    transition: all 0.3s;
  }
  .slide-enter, .slide-leave-to{
    transform: translateX(100%);
  }
}
</style>
